package learn.basic;

import java.io.File;
import java.io.FileNotFoundException;

import org.gephi.data.attributes.api.AttributeController;
import org.gephi.data.attributes.api.AttributeModel;
import org.gephi.filters.api.FilterController;
import org.gephi.filters.api.Query;
import org.gephi.filters.api.Range;
import org.gephi.filters.plugin.graph.DegreeRangeBuilder;
import org.gephi.filters.plugin.graph.DegreeRangeBuilder.DegreeRangeFilter;
import org.gephi.filters.plugin.graph.EgoBuilder.EgoFilter;
import org.gephi.filters.plugin.operator.INTERSECTIONBuilder.IntersectionOperator;
import org.gephi.filters.plugin.partition.PartitionBuilder;
import org.gephi.filters.plugin.partition.PartitionBuilder.NodePartitionFilter;
import org.gephi.graph.api.DirectedGraph;
import org.gephi.graph.api.GraphController;
import org.gephi.graph.api.GraphModel;
import org.gephi.graph.api.GraphView;
import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.api.ImportController;
import org.gephi.io.processor.plugin.DefaultProcessor;
import org.gephi.partition.api.Partition;
import org.gephi.partition.api.PartitionController;
import org.gephi.project.api.ProjectController;
import org.gephi.project.api.Workspace;
import org.openide.util.Lookup;

public class ManipulateFilter {

	@SuppressWarnings("rawtypes")
	public static void main(String[] args) {
		ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
		pc.newProject();
		Workspace workspace = pc.getCurrentWorkspace();
		
		ImportController importController = Lookup.getDefault().lookup(ImportController.class);
		GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
		AttributeModel attributeModel = Lookup.getDefault().lookup(AttributeController.class).getModel();
		
		Container container;
		try {
			File file = new File("Data/polblogs.gml");
			container = importController.importFile(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		}
		
		importController.process(container, new DefaultProcessor(), workspace);
		
		FilterController filterController = Lookup.getDefault().lookup(FilterController.class);
		DegreeRangeFilter degreeFilter = new DegreeRangeBuilder().getFilter();
		degreeFilter.init(graphModel.getGraph());
		degreeFilter.setRange(new Range(10, Integer.MAX_VALUE));
		Query query = filterController.createQuery(degreeFilter);
		GraphView view = filterController.filter(query);
		graphModel.setVisibleView(view);
		
		DirectedGraph graph = graphModel.getDirectedGraphVisible();
		System.out.println("Nodes: " + graph.getNodeCount() + " Edges: " + graph.getEdgeCount());
		
		PartitionController partitionController = Lookup.getDefault().lookup(PartitionController.class);
		Partition p = partitionController.buildPartition(attributeModel.getNodeTable().getColumn("source"), graph);
		NodePartitionFilter partitionFilter = new PartitionBuilder.NodePartitionFilter(p);
		partitionFilter.unselectAll();
		partitionFilter.addPart(p.getPartFromValue("Blogarama"));
		Query query2 = filterController.createQuery(partitionFilter);
		GraphView view2 = filterController.filter(query2);
		graphModel.setVisibleView(view2);
		
		graph = graphModel.getDirectedGraphVisible();
		System.out.println("Nodes: " + graph.getNodeCount() + " Edges: " + graph.getEdgeCount());
		
		IntersectionOperator intersectionOperator = new IntersectionOperator();
		Query query3 = filterController.createQuery(intersectionOperator);
		filterController.setSubQuery(query3, query);
		filterController.setSubQuery(query3, query2);
		GraphView view3 = filterController.filter(query3);
		graphModel.setVisibleView(view3);
		
		graph = graphModel.getDirectedGraphVisible();
		System.out.println("Nodes: " + graph.getNodeCount() + " Edges: " + graph.getEdgeCount());
		
		EgoFilter egoFilter = new EgoFilter();
		egoFilter.setPattern("obamablog.com");
		egoFilter.setDepth(1);
		Query queryEgo = filterController.createQuery(egoFilter);
		GraphView viewEgo = filterController.filter(queryEgo);
		graphModel.setVisibleView(viewEgo);
		
		graph = graphModel.getDirectedGraphVisible();
		System.out.println("Nodes: " + graph.getNodeCount() + " Edges: " + graph.getEdgeCount());
	}

}